﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BuXiaChu.OrderDishes.BLL;
using BuXiaChu.OrderDishes.BLL.Constants;
using BuXiaChu.OrderDishes.Model;

namespace BuXiaChu.OrderDishes.Web.Admin.ServiceStation
{
    public partial class Add : System.Web.UI.Page
    {
        protected int saveStatus = -1;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack == false)
            {
                InitData();
            }
            else
            {
                Save();
            }
        }

        private void InitData()
        {
            #region

            foreach (var item in CodeDictionary.Instance.GetAllArea())
            {
                if (item.ParentId == 0)
                {
                    ListItem li = new ListItem(item.Name, item.Id.ToString());
                    sel_City.Items.Add(li);
                }
            }

            foreach (var item in CodeDictionary.Instance.GetListByClassID(CodeDictionaryConstants.ServiceStationLevel))
            {
                ListItem li = new ListItem(item.Name, item.CodeID.ToString());
                sel_Level.Items.Add(li);
            }

            var managerList = EmployeesManager.GetEmployeesByJobTitle(27);
            sel_Manager.Items.Add(new ListItem("请选择", "-1"));
            foreach (var item in managerList)
            {
                ListItem li = new ListItem(item.Name, item.Id.ToString());
                sel_Manager.Items.Add(li);
            }

            var AssistantList = EmployeesManager.GetEmployeesByJobTitle(28);
            sel_Assistant.Items.Add(new ListItem("请选择", "-1"));
            foreach (var item in AssistantList)
            {
                ListItem li = new ListItem(item.Name, item.Id.ToString());
                sel_Assistant.Items.Add(li);
            }

            #endregion

            #region
            var id = 0;
            if (int.TryParse(Request.QueryString["id"], out id))
            {
                ServiceStationModel entity = ServiceStationManager.GetServiceStationById(id);
                txt_Name.Value = entity.Name;
                sel_City.Value = entity.CityId.ToString();
                txt_AreaId.Value = entity.AreaId.ToString();
                sel_Level.Value = entity.Level.ToString();
                txt_Address.Value = entity.Address;
                txt_Phone.Value = entity.Tel;
                sel_Manager.Value = entity.ManagerId.ToString();
                sel_Assistant.Value = entity.AssistantId.ToString();
                txt_Comment.Value = entity.Remarks;
            }
            #endregion
        }

        private void Save()
        {
            #region
            var id = 0;
            int.TryParse(Request.QueryString["id"], out id);
            ServiceStationModel entity = new ServiceStationModel();
            entity.SSId = id;
            entity.Name = txt_Name.Value;
            entity.CityId = int.Parse(sel_City.Value);
            entity.AreaId = int.Parse(txt_AreaId.Value);
            entity.Level = int.Parse(sel_Level.Value);
            entity.Address = txt_Address.Value.Trim();
            entity.Tel = txt_Phone.Value.Trim();
            entity.ManagerId = int.Parse(sel_Manager.Value);
            entity.AssistantId = int.Parse(sel_Assistant.Value);
            entity.Remarks = txt_Comment.Value.Trim();
            if (ServiceStationManager.Save(entity))
            {
                saveStatus = 1;
            }
            else
            {
                saveStatus = 0;
            }
            #endregion
        }
    }
}
